/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.genew.squadra.xml.model.builder;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.genew.squadra.xml.model.toc.Head;
import org.genew.squadra.xml.model.toc.Navmap;
import org.genew.squadra.xml.model.toc.Navpoint;
import org.genew.squadra.xml.model.toc.Toc;

/**
 *
 * @author giona_000
 */
public class TocBuilder implements Builder{
    
    public static void buildToc(Toc toc, XMLStreamWriter xmlToc) throws XMLStreamException{
    xmlToc.writeStartDocument("UTF-8","1.0");
    xmlToc.writeStartElement("ncx:ncx");
    xmlToc.writeAttribute("xmlns:ncx", "http://www.daisy.org/z3986/2005/ncx/");
    xmlToc.writeAttribute("version", "2005-1");
    
    buildHead(toc.getHead(), xmlToc);
    
    xmlToc.writeStartElement("ncx:docTitle");
        xmlToc.writeStartElement("ncx:text");
        xmlToc.writeCharacters(toc.getDocTitle());
        xmlToc.writeEndElement();
    xmlToc.writeEndElement();

    xmlToc.writeStartElement("ncx:docAuthor");
        xmlToc.writeStartElement("ncx:text");
        xmlToc.writeCharacters(toc.getDocAuthor());
        xmlToc.writeEndElement();
    xmlToc.writeEndElement();
        buildNavMap(toc.getNavmap(), xmlToc);
    xmlToc.writeEndElement();
    xmlToc.writeEndDocument();
    }
    
    private static void buildHead(Head head, XMLStreamWriter xmlToc) throws XMLStreamException{
    xmlToc.writeStartElement("ncx:head");
                
                    xmlToc.writeStartElement("ncx:meta");
                        xmlToc.writeAttribute("name", "dtb:uid");
                        xmlToc.writeAttribute("content", head.getUid());
                    xmlToc.writeEndElement();
                    
                    xmlToc.writeStartElement("ncx:meta");
                        xmlToc.writeAttribute("name", "dtb:depth");
                        Integer depth = head.getDepht();
                        xmlToc.writeAttribute("content",depth.toString() );
                    xmlToc.writeEndElement();
                    
                    xmlToc.writeStartElement("ncx:meta");
                        xmlToc.writeAttribute("name", "dtb:totalPageCount");
                        Integer tPn = head.getTotalPageCount();
                        xmlToc.writeAttribute("content",tPn.toString());
                    xmlToc.writeEndElement();
                    
                    xmlToc.writeStartElement("ncx:meta");
                        xmlToc.writeAttribute("name", "dtb:maxPageNumber");
                        Integer mPn = head.getMaxPageNumber();
                        xmlToc.writeAttribute("content", mPn.toString());
                    xmlToc.writeEndElement();
                
    xmlToc.writeEndElement();    
    }
    private static void buildNavMap(Navmap navmap, XMLStreamWriter xmlToc) throws XMLStreamException{
    xmlToc.writeStartElement("ncx:navMap");
        int numNP = navmap.getNavpoints().size();
        for (int i = 0; i < numNP ; i++){
        Navpoint temPoint = navmap.getNavpoints().get(i);    
        xmlToc.writeStartElement("ncx:navPoint");    
        xmlToc.writeAttribute("id", temPoint.getId());
        Integer temp = temPoint.getPlayOrder();
        xmlToc.writeAttribute("playOrder",temp.toString());
            xmlToc.writeStartElement("ncx:navLabel"); 
                xmlToc.writeStartElement("ncx:text");
                xmlToc.writeCharacters(temPoint.getNavLaberl());
                xmlToc.writeEndElement();
            xmlToc.writeEndElement();
          xmlToc.writeStartElement("ncx:content");
          xmlToc.writeAttribute("src",temPoint.getContent());
        xmlToc.writeEndElement();
        xmlToc.writeEndElement();   
        }
    xmlToc.writeEndElement();    
    }

    
    
}
